
var prefix = "/fin/billing"
$(function() {
	load();
	laydate({
		elem : '#invoiceTimeend'
	});
	laydate({
		elem : '#invoiceTimeStr'
	});
});

function load() {
	$('#exampleTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						singleSelect : false, // 设置为true将禁止多选
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						showColumns : false, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							return {
								//说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
								limit: params.limit,
								offset:params.offset,
								invoiceTimeend:$('#invoiceTimeend').val(),
								invoiceTimeStr:$('#invoiceTimeStr').val(),
								projectNumber:$('#projectNumber').val(),
								projectName:$('#projectName').val(),
								billingComplayName:$('#billingComplayName').val(),
								receiptName:$('#receiptName').val(),
								taxRate:$('#taxRate').val(),
								userNameCreate:$('#userNameCreate').val()
							};
						},
						columns : [
								{
									checkbox : true
								},
								{
									visible : false,
									field : 'id', 
									title : '主键' 
								},
								{
									field : 'invoiceCode',
									title : '发票代码'
								},
								{
									field : 'status',
									title : '状态',
									formatter : function(value, row, index){
										if(value==1){
											return '<span class="label label-primary">新创建</span>';
										}else if(value==2){
											return '<span class="label label-primary">等待开票</span>';
										}else if(value==3){
											return '<span class="label label-primary">已开票</span>';
										}else if(value==4){
											return '<span class="label label-danger">作废</span>';
										}
									}
								},
								{
									field : 'invoiceNumber', 
									title : '发票号码' 
								},
								{
									field : 'invoiceTime', 
									title : '发票日期' 
								},
								{
									visible : false,
									field : 'projectId', 
									title : '项目ID' 
								},
								{
									field : 'projectName', 
									title : '项目名称' 
								},
								{
									field : 'projectNumber', 
									title : '合同编号' 
								},
								{
									visible : false,
									field : 'billingComplayId', 
									title : '开票方ID' 
								},
								{
									field : 'billingComplayName', 
									title : '开票方' 
								},
								{
									field : 'receiptName', 
									title : '收票方' 
								},
								{
									visible : false,
									field : 'invoiceTypeid', 
									title : '发票类型id' 
								},
								{
									field : 'invoiceType', 
									title : '发票类型' 
								},
								{
									field : 'taxRate', 
									title : '税率' 
								},
								{
									field : 'netAmount', 
									title : '净额' 
								},
								{
									field : 'taxAmount', 
									title : '税额' 
								},
								{
									field : 'invoiceAmount', 
									title : '票额',
									formatter: function(value,row,index){
										var invoiceAmount =parseFloat(row.invoiceAmount).toFixed(2);
										return '<a href="/fin/billingFile/getFiles/'+row.id+'" target="_blank">'+invoiceAmount+'</a>';
									}
								},
								{
									field : 'invoiceDescr', 
									title : '开票内容' 
								},
								{
									field : 'billingPic',
									title : '发票照片',
									formatter: function (value, row,index ) {
										return '<img  style="width: 100px;   height: 80px;    margin-top: -6px;cursor: pointer;" src='+row.billingPic+' alt="无" onclick="bimges(this) ">'
									}
								},
								{
									field : 'remarks', 
									title : '备注' 
								},
								{
									visible : false,
									field : 'userIdCreate', 
									title : '创建人ID' 
								},
								{
									field : 'userNameCreate', 
									title : '创建人' 
								},
								{
									field : 'gmtCreate', 
									title : '创建时间' 
								},
								{
									title : '操作',
									field : 'operation',
									align : 'center',
									formatter : function(value, row, index) {
										var up = '<a class="btn btn-info btn-sm " href="#" title="上传文件"  mce_href="#" onclick="uploadInfo(\''
											+ row.id
											+ '\')"><i class="fa fa-upload"></i></a> ';
										if(row.status==1){
											var e = '<a class="btn btn-primary btn-sm '+s_edit_h+'" href="#" mce_href="#" title="编辑" onclick="edit(\''
												+ row.id
												+ '\')"><i class="fa fa-edit"></i></a> ';
											var d = '<a class="btn btn-warning btn-sm '+s_remove_h+'" href="#" title="删除"  mce_href="#" onclick="remove(\''
												+ row.id
												+ '\')"><i class="fa fa-trash-o"></i></a> ';
											var f = '<a class="btn btn-danger btn-sm '+s_verify_h+'" href="#" title="审核"  mce_href="#" onclick="check(\''
												+ row.id
												+ '\')"><i class="fa fa-hand-paper-o"></i></a> ';
											return e + d + f+up;
										}else if(row.status==2){
											var e = '<a class="btn btn-primary btn-sm  btn-sm '+s_verify_h+'" href="#" mce_href="#" title="编辑" onclick="edit(\''
												+ row.id
												+ '\')"><i class="fa fa-edit"></i></a> ';
											var f = '<a class="btn btn-danger btn-sm '+s_verify_h+'" href="#" title="开票"  mce_href="#" onclick="billingcheck(\''
												+ row.id
												+ '\')"><i class="fa fa-key"></i></a> ';
											return  e+f+up;
										}
									}
								} ]
					});
}
function reLoad() {
	$('#exampleTable').bootstrapTable('refresh');
}
function add() {
	layer.open({
		type : 2,
		title : '增加',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
}
function edit(id) {
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/edit/' + id // iframe的url
	});
}
function remove(id) {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/remove",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}
//审核
function check(id) {
	layer.confirm('确认需要开票吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/check",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

//开票
function billingcheck(id) {
	layer.confirm('确认开票吗？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : prefix+"/billingcheck",
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					reLoad();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}


function exportData() {
	var url =prefix + '/exportDataBylist';
	var invoiceTimeend=$('#invoiceTimeend').val();
	var	invoiceTimeStr=$('#invoiceTimeStr').val();
	var	projectNumber=$('#projectNumber').val();
	var	projectName=$('#projectName').val();
	var	billingComplayName=$('#billingComplayName').val();
	var	receiptName=$('#receiptName').val();
	var	taxRate=$('#taxRate').val();
	var	userNameCreate=$('#userNameCreate').val();
	if(invoiceTimeend != null){
		if (url.indexOf('?') > -1) {
			url += '&invoiceTimeend=' + invoiceTimeend;
		} else {
			url += '?invoiceTimeend=' + invoiceTimeend;
		}
	}
	if(invoiceTimeStr != null){
		if (url.indexOf('?') > -1) {
			url += '&invoiceTimeStr=' + invoiceTimeStr;
		} else {
			url += '?invoiceTimeStr=' + invoiceTimeStr;
		}
	}
	if(projectNumber != null){
		if (url.indexOf('?') > -1) {
			url += '&projectNumber=' + projectNumber;
		} else {
			url += '?projectNumber=' + projectNumber;
		}
	}
	if(projectName != null){
		if (url.indexOf('?') > -1) {
			url += '&projectName=' + projectName;
		} else {
			url += '?projectName=' + projectName;
		}
	}
	if(billingComplayName != null){
		if (url.indexOf('?') > -1) {
			url += '&billingComplayName=' + billingComplayName;
		} else {
			url += '?billingComplayName=' + billingComplayName;
		}
	}
	if(receiptName != null){
		if (url.indexOf('?') > -1) {
			url += '&receiptName=' + receiptName;
		} else {
			url += '?receiptName=' + receiptName;
		}
	}
	if(taxRate != null){
		if (url.indexOf('?') > -1) {
			url += '&taxRate=' + taxRate;
		} else {
			url += '?taxRate=' + taxRate;
		}
	}
	if(userNameCreate != null){
		if (url.indexOf('?') > -1) {
			url += '&userNameCreate=' + userNameCreate;
		} else {
			url += '?userNameCreate=' + userNameCreate;
		}
	}
	var form = document.createElement('form');
	form.setAttribute('action', url);
	form.setAttribute('method', 'post');
	form.setAttribute('style', 'display:none');
	document.body.appendChild(form);
	form.submit();
	document.body.removeChild(form)
}

//点击展示大图
function bimges(val) {
	var src = $(val).prop('src')

	var img = '<img  style="width: 100%;   height: 100%;" src='+src+' alt="无">'
	$("#bigimges").html(img)
	$("#bigimges").show()

}
$("#bigimges").click(function () {
	console.log(11)
	$("#bigimges").hide()
})


function uploadInfo(id) {
	var index = layer.open({
		type : 2,
		title : '上传文件',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/uploadInfo/' + id // iframe的url
	});
	layer.full(index);
}